import {
    app,
    BrowserWindow,
    ipcMain,
    Menu,
    MenuItemConstructorOptions,
} from "electron";

ipcMain.on("quit", () => {
    const template = [
        {
            label: "退出",
            click: () => {
                app.quit();
            },
        },
    ] as MenuItemConstructorOptions[];

    const menu = Menu.buildFromTemplate(template);
    menu.popup();
});

/**pinia多窗口共享 */
ipcMain.handle(
    "pinia-store-change",
    (event, storeName: string, jsonStr: string) => {
        // 遍历window执行
        for (const currentWin of BrowserWindow.getAllWindows()) {
            const webContentsId = currentWin.webContents.id;
            if (webContentsId !== event.sender.id) {
                currentWin.webContents.send(
                    "pinia-store-set",
                    storeName,
                    jsonStr
                );
            }
        }
    }
);

ipcMain.on("set-ignore-mouse-events", (event, args) => {
    const win = BrowserWindow.fromWebContents(event.sender);
    if (win) win.setIgnoreMouseEvents(args);
});
